plot_ly(
x = c(1, 2, 3),
y = c(5, 7, 8),
type = "scatter",
mode = "lines"
)plot_ly(
x = c(1, 2, 3),
y = c(5, 7, 8),
type = "scatter",
mode = "markers",
size = c(2, 5, 10),
marker = list(color = c("red", "blue", "green"))
)plot_ly(
data = iris,
x = ~Petal.Length,
y = ~Petal.Width,
color = ~Species,
type = "scatter",
mode = "markers"
)条形图横轴默认按字母排序,要加入categoryorder = "array"选项,才会按给定的顺序排序
plot_ly(
x = c("C", "B", "A"),
y = c(5, 10, 8),
type = "bar"
) %>%
layout(xaxis = list(categoryarray = ~names, categoryorder = "array"))class(volcano)#> [1] "matrix" "array"
str(volcano)#> num [1:87, 1:61] 100 101 102 103 104 105 105 106 107 108 ...
plot_ly(z = volcano, type = "heatmap")# 若矩阵的行列均为分类变量,则需要标准矩阵的行、列名
# plot_ly(
# x = rownames(main_intimate_matrix),
# y = colnames(main_intimate_matrix),
# z = main_intimate_matrix,
# type = "heatmap"
# )plot_ly(
x = c(1, 2, 3),
y = c(5, 6, 8),
type = "scatter",
mode = "lines",
fill = "tozeroy" # from line down to x axis
)x <- rchisq(100, 5, 0)
x#> [1] 2.3247535 0.1698598 1.8166828 1.9083598 6.2974274 2.2367440
#> [7] 4.8942547 2.0345038 6.0826935 3.2335586 6.8466034 2.9081385
#> [13] 4.4941843 3.8188119 5.7685094 4.3077927 5.6252842 8.2170908
#> [19] 6.7764092 4.6184388 4.3988233 7.6843370 3.8873698 9.1499021
#> [25] 5.2242807 8.0127810 4.5352382 3.4965635 1.4014437 6.3333595
#> [31] 11.4734674 10.5375938 7.9668736 1.8101643 1.5068191 7.7703736
#> [37] 3.4705912 2.9557524 3.2394801 2.2546457 2.9695268 3.5033811
#> [43] 5.1087119 2.3899103 9.3736024 10.2590591 0.9262337 3.9778088
#> [49] 1.6893493 0.7269512 6.5948703 9.4090179 1.5034732 4.1015528
#> [55] 3.2485875 5.7385417 4.1843394 3.3472970 6.5364108 3.6791770
#> [61] 2.7187530 12.4701629 2.5466233 6.9976631 7.8312196 2.3357896
#> [67] 7.8656306 3.4785455 2.3101135 3.5892443 7.2031077 0.7612959
#> [73] 6.2576455 1.4767935 5.3095203 6.0719609 11.6408995 8.5116271
#> [79] 3.7826044 1.5857094 3.1952146 5.6164815 4.8599728 4.9711235
#> [85] 3.8824789 3.9028379 11.4647334 8.6638342 4.4558529 9.8403664
#> [91] 4.6297470 10.6350155 2.1335926 9.4727930 8.0280484 2.5612757
#> [97] 7.4359554 11.9119274 2.0361541 6.7461621
plot_ly(x = x, type = "histogram")plot_ly(y = rnorm(50), type = "box") %>%
add_trace(y = rnorm(50, 1))plot_ly(midwest, x = ~percollege, color = ~state, type = "box")plot_ly(
x = rnorm(1000, sd = 10),
y = rnorm(1000, sd = 5),
type = "histogram2d"
)plot_ly(
type = "scattergeo",
lon = c(-73.5, 151.2),
lat = c(45.5, -33.8),
marker = list(color = c("red", "blue"), size = c(30, 50), mode = "markers")
)plot_ly(
type = "choropleth",
locations = c("AZ", "CA", "VT"),
locationmode = "USA-states",
colorscale = "Viridis",
z = c(10, 20, 40)
) %>%
layout(geo = list(scope = "usa"))plot_ly(
type = "scattergeo",
lon = c(42, 39),
lat = c(12, 22),
text = c("Rome", "Greece"),
mode = "markers"
)surface,~matrixz = outer(x, y, .f) %>% t(),一定要转置!# 不指定x和y时,3D surface图的x轴和y轴分别是矩阵列和行的index,从0开始间隔1计算
plot_ly(type = "surface", z = ~volcano)# 指定x和y的标准3D曲面生成流程:关键函数 outer(x, y, .f) %>% t()
library(tidyverse)
library(plotly)
library(htmlwidgets)
scale <- 1000
sigmoid <- function(x) {
1 / (1 + exp(-x))
}
cal_z <- function(x, y) {
10 * x - 15 * y
}
cal_a <- function(x, y) {
cal_z(x, y) %>% sigmoid()
}
x1 <- runif(scale) %>% sort()
y1 <- runif(scale) %>% sort()
z1 <- outer(x1, y1, cal_a) %>% t()
p1 <- plot_ly(type = "surface", x = x1, y = y1, z = ~z1)
saveWidget(p1, "../figure/sigmoid曲面1.html", selfcontained = F, libdir = "lib")
p1